package com.andy.wan.android.mvp.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;

import com.andy.ui.base.BaseListFragment;
import com.andy.ui.base.SimpleWebActivity;
import com.andy.ui.widget.PageStateView;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.api.WanAndroidApi;
import com.andy.wan.android.mvp.bean.ArticleBean;
import com.andy.wan.android.mvp.bean.BannerBean;
import com.andy.wan.android.mvp.bean.BaseResponse;
import com.andy.wan.android.mvp.bean.HomeSummaryBean;
import com.andy.wan.android.mvp.bean.PageBean;
import com.andy.wan.android.mvp.net.RetrofitUtil;
import com.andy.wan.android.mvp.ui.adapter.HomeAdapter;
import com.andy.wan.android.mvp.ui.adapter.HomeBannerAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.config.BannerConfig;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.util.BannerUtils;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/17 9:52
 * @Description:
 */
public class HomeFragment extends BaseListFragment<HomeSummaryBean, ArticleBean> {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;
    @BindView(R.id.page_stat_view)
    PageStateView mPageStatView;
    private HomeAdapter mHomeAdapter;
    private Banner<?, HomeBannerAdapter> mBanner;
    private HomeBannerAdapter mHomeBannerAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @NonNull
    @Override
    protected Observable<HomeSummaryBean> getObservable() {
        Observable<BaseResponse<List<BannerBean>>> bannerObservable = RetrofitUtil.getInstance().create(WanAndroidApi.class).banner();
        Observable<BaseResponse<PageBean<ArticleBean>>> articleObservable = RetrofitUtil.getInstance().create(WanAndroidApi.class).articleList(getPageNum());
        return Observable.zip(bannerObservable, articleObservable, new BiFunction<BaseResponse<List<BannerBean>>, BaseResponse<PageBean<ArticleBean>>, HomeSummaryBean>() {
            @Override
            public HomeSummaryBean apply(BaseResponse<List<BannerBean>> listBaseResponse, BaseResponse<PageBean<ArticleBean>> pageBeanBaseResponse) throws Exception {
                HomeSummaryBean summaryBean = new HomeSummaryBean();
                summaryBean.setBannerBeanList(listBaseResponse.getData());
                summaryBean.setArticleBeanList(pageBeanBaseResponse.getData().getDatas());
                return summaryBean;
            }
        });
    }

    @Override
    protected void onGetData(HomeSummaryBean result) {
        super.onGetData(result);
        mHomeBannerAdapter.setDatas(result.getBannerBeanList());
        mHomeBannerAdapter.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                BannerBean bannerBean = (BannerBean) data;
                SimpleWebActivity.startMe(getContext(), bannerBean.getUrl(), bannerBean.getTitle());
            }

            @Override
            public void onBannerChanged(int position) {

            }
        });
        mBanner.setAdapter(mHomeBannerAdapter);
    }

    @Override
    protected List<ArticleBean> getRecyclerDataFromResult(HomeSummaryBean result) {
        return result.getArticleBeanList();
    }

    @NonNull
    @Override
    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @Nullable
    @Override
    protected SmartRefreshLayout getRefreshLayout() {
        return mSmartRefresh;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<ArticleBean, BaseViewHolder> getAdapter() {
        mHomeAdapter = new HomeAdapter();
        mHomeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ArticleBean articleBean = mHomeAdapter.getItem(position);
                if (articleBean != null) {
                    SimpleWebActivity.startMe(getContext(), articleBean.getLink(), articleBean.getTitle());
                }
            }
        });
        initBanner();
        return mHomeAdapter;
    }

    private void initBanner() {
        View headerView = LayoutInflater.from(getContext()).inflate(R.layout.layout_home_banner, null);
        mHomeAdapter.setHeaderView(headerView);
        mBanner = headerView.findViewById(R.id.banner);
        mHomeBannerAdapter = new HomeBannerAdapter();
        mBanner.setIndicator(new CircleIndicator(getContext()));
        mBanner.setIndicatorGravity(IndicatorConfig.Direction.RIGHT);
        mBanner.setIndicatorMargins(new IndicatorConfig.Margins(0, 0,
                BannerConfig.INDICATOR_MARGIN, (int) BannerUtils.dp2px(12)));

    }

    @Override
    protected PageStateView getPageStateView() {
        return mPageStatView;
    }
}
